package com.augurit.agmobile.agwater5.drainage.supervise.adapter

import android.graphics.drawable.ColorDrawable
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.augurit.agmobile.agwater5.drainage.common.ex.toColorSpan
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseVariousModuleBean
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseVariousSubModuleBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.drainage.R
import com.example.drainage.base.ktexpand.visible

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.supervise.adapter
 * @createTime 创建时间 ：2022/1/5
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class SuperviseVariousModuleAdapter : BaseQuickAdapter<SuperviseVariousModuleBean, BaseViewHolder>(R.layout.item_supervise_various_module) {

    private val countColor = "#FF646A73".toColorInt()
    private val startStr = "总数 "
    private val middleStr = "  已检查 "

    override fun convert(holder: BaseViewHolder, item: SuperviseVariousModuleBean) {
        holder.setText(R.id.tvModuleName, item.moduleName)
        holder.setText(R.id.tvModuleCount, getModuleCountChar(item.moduleTotalCount, item.moduleSuperviseCount))
        val recyclerView = holder.getView<RecyclerView>(R.id.moduleRecyclerView)
        recyclerView.visible = item.moduleObj != null
        if (item.moduleObj != null) {
            val adapter = if (recyclerView.adapter == null) {
                SuperviseVariousSubModuleAdapter().also {
                    val dividerItemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
                    dividerItemDecoration.setDrawable(ColorDrawable("#FFEFF0F1".toColorInt()))
                    recyclerView.addItemDecoration(dividerItemDecoration)
                    recyclerView.adapter = it
                }
            } else {
                recyclerView.adapter as SuperviseVariousSubModuleAdapter
            }
            if (item.subModuleList == null) {
                // 数据转换
                item.subModuleList = mutableListOf(
                    SuperviseVariousSubModuleBean("组织机构、制度建设及落实制度建设及落实制度建设及落实", 1, 5, false, 11),
                    SuperviseVariousSubModuleBean("水库大坝安全责任制", 11, 15, false, 11),
                    SuperviseVariousSubModuleBean("信息档案管理", 21, 25, false, 11),
                )
            }
            adapter.setNewInstance(item.subModuleList)
        }
    }

    private fun getModuleCountChar(totalCount: Int, superviseCount: Int): CharSequence {
        val str = "$startStr${totalCount}$middleStr${superviseCount}"
        val totalLength = totalCount.toString().length
        val superviseLength = superviseCount.toString().length
        return str.toColorSpan(startStr.length..(startStr.length + totalLength), countColor)
            .toColorSpan((str.length - superviseLength)..str.length, countColor)
    }
}